<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :background="background"
      v-model:currentPage="currentPage"
      v-model:pageSize="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { toRefs, watch, defineEmits } from 'vue'
import { scrollTo } from '@/utils/scroll-to'

const props = defineProps({
  total: Number,
  page: {
    type: Number,
    default: 1
  },
  limit: {
    type: Number,
    default: 20
  },
  pageSizes: {
    type: Array,
    default: () => [10, 20, 30, 50]
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper'
  },
  background: {
    type: Boolean,
    default: true
  },
  autoScroll: {
    type: Boolean,
    default: true
  },
  hidden: {
    type: Boolean,
    default: false
  }
})

const { page: currentPage, limit: pageSize } = toRefs(props)
const emits = defineEmits(['update:page', 'update:limit', 'pagination'])

watch(currentPage, newPage => {
  emits('update:page', newPage)
})

watch(pageSize, newSize => {
  emits('update:limit', newSize)
})

const handleSizeChange = val => {
  emits('pagination', { page: currentPage.value, limit: val })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}

const handleCurrentChange = val => {
  emits('pagination', { page: val, limit: pageSize.value })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}
</script>

<style>
.pagination-container {
  margin-top: 0;
  background: #fff;
  padding: 0 16px;
}

.pagination-container.hidden {
  display: none;
}

.page-style {
  line-height: 32px;
  height: 32px;
  margin-top: 15px;
  font-size: 14px;
  color: #8c8c8c;
  letter-spacing: -0.43px;
}
</style>
